package com.Web.Servlet;

import com.Web.Person.BanJi;
import com.Web.Service.IClassService;
import com.Web.Service.Impl.ClassServiceImpl;
import com.Web.util.PageInfo;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/banji")
public class ClassServlet extends HttpServlet {

    //private IClassDAO classDAO = new ClassDAOImpl();
    private IClassService classService = new ClassServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if(method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "selectAll":
                selectAll(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "selectBanJiAddPage":
                selectBanJiAddPage(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "toUpdate":
                toUpdate(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            case "selectByPage":
                selectByPage(req,resp);
                break;
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("ClassServlet.update");
        String ids = req.getParameter("id");
        int id = Integer.parseInt(ids);
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        BanJi banji = new BanJi(id, name, address);
        classService.update(banji);
        resp.sendRedirect("/banji");
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ClassServlet.toUpdate");
        String ids = req.getParameter("id");
        int id = Integer.parseInt(ids);
        BanJi banji = classService.toUpdate(id);
        req.setAttribute("banji",banji);
        req.getRequestDispatcher("/class_update.jsp").forward(req,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ClassServlet.add");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        BanJi banji = new BanJi(name,address);
        classService.add(banji);
        resp.sendRedirect("/banji");
    }

    private void selectBanJiAddPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ClassServlet.selectBanJiAddPage");
        req.getRequestDispatcher("/class_add.jsp").forward(req,resp);
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("ClassServlet.deleteById");
        String id = req.getParameter("id");
        System.out.println("id:" + id);
        int i = Integer.parseInt(id);
        classService.deleteById(i);
        resp.sendRedirect("/banji");
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ClassServlet.selectAll");
        List<BanJi> list = classService.selectAll();
        for(BanJi banji : list){
            System.out.println(banji);
        }
        resp.setContentType("text/html;charset=UTF-8");
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(resp.getWriter(),list);
//        req.setAttribute("list",list);
//        req.getRequestDispatcher("class_list.jsp").forward(req,resp);
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ClassServlet.selectByPage");
        String pageNo = req.getParameter("pageNo");
        if(pageNo == null || pageNo.equals("")) {
            pageNo = "1";
        }
        String pageSize = req.getParameter("pageSize");
        if(pageSize == null || pageSize.equals("")) {
            pageSize = "5";
        }
        PageInfo pageInfo = classService.selectByPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        System.out.println(pageInfo);
        //req.setAttribute("list",list);
        //req.setAttribute("totalPage",totalPage);
        //req.setAttribute("pageNo",pageNo);
        //req.setAttribute("pageSize",pageSize);
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("class_list.jsp").forward(req,resp);
    }
}
